
*Load 2016 version of HNPI consolidation file - available with HCCI claims  
import excel "$input\hosp_hnpi_consolidation_201606.xlsx", sheet("aha_hnpi_xwalk") firstrow clear
drop if hnpi == ""
drop if hnpi == "F11E226E4C5B78E02FCB9925D46A52DA" & consolidated_hnpi == "F11E226E4C5B78E02FCB9925D46A52DA"
duplicates tag hnpi, gen(dup)
assert dup == 0
drop dup
keep hnpi consolidated_hnpi PRIMARY_ZIP
ren (consolidated_hnpi PRIMARY_ZIP) (aha_hnpi aha_zip)
save "$temp\aha_crosswalk.dta", replace

*Process 2011-2013 claims separately from 2014 because of changes in data structure
foreach yr in 11 12 13 {

use "$temp\ipclaims`yr'.dta", replace

	merge m:1 hnpi using "$temp\aha_crosswalk.dta", keepus(aha_hnpi)
	drop if _m == 2
	drop _m hnpi hnpi_re

	save "$temp\IP claims processed with AHA HNPI - 20`yr'.dta", replace
}

foreach yr in 14 {

use "$temp\ipclaims`yr'.dta", replace

	merge m:1 hnpi using "$temp\aha_crosswalk.dta", keepus(aha_hnpi)
	preserve
		keep if _m == 1
		drop _m hnpi aha_hnpi
		save "$temp\hnpi no-match`yr'.dta", replace
	restore
	
	keep if _m == 3
	drop _m hnpi
	
	save "$temp\Temp - IP claims processed with AHA HNPI - 20`yr'.dta", replace
	
	use "$temp\hnpi no-match`yr'.dta", clear
	ren hnpi_re hnpi
	merge m:1 hnpi using "$temp\aha_crosswalk.dta", keepus(aha_hnpi)
	preserve
		keep if _m == 1
		drop _m hnpi aha_hnpi
		save "$temp\hnpi no-match`yr'.dta", replace
	restore

	keep if _m == 3
	drop _m hnpi
	
	append using "$temp\Temp - IP claims processed with AHA HNPI - 20`yr'.dta"
	save "$temp\Temp - IP claims processed with AHA HNPI - 20`yr'.dta", replace
	
	use "$temp\aha_crosswalk.dta", clear
	keep aha_zip aha_hnpi
	duplicates tag aha_zip, gen(dup)
	drop if dup>0
	drop dup
	save "$temp\unique aha_hnpi.dta", replace
	clear
	
	use "$temp\hnpi no-match`yr'.dta", clear
	gen str5 aha_zip = string(prov_zip_5_cd, "%05.0f")
	merge m:1 aha_zip using "$temp\unique aha_hnpi.dta", keepus(aha_hnpi)
	drop if _m == 2
	drop _m aha_zip
	
	append using "$temp\Temp - IP claims processed with AHA HNPI - 20`yr'.dta"
	save "$temp\IP claims processed with AHA HNPI - 20`yr'.dta", replace
	
}

exit






